package com.paradise.ytj.helper;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import com.paradise.ytj.bean.ContactInfo;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;


/**
 * Created by Administrator on 2017/8/15 0015.
 */

public class RecordDao {

    private MyDBOpenHelper dbOpenHelper;

    // 在RecordDao被new出来的时候 就完成初始化
    public RecordDao(Context context) {
        dbOpenHelper = new MyDBOpenHelper(context);
    }

    /**
     * 往数据库添加一条数据
     */
    public boolean add(String name, String account, String time, String call) {
        SQLiteDatabase db = dbOpenHelper.getWritableDatabase();
        long record = 0;
        if (db.isOpen()) {
            //方法二
            ContentValues values = new ContentValues();
            values.put("name", name);
            values.put("account", account);
            values.put("time", time);
            values.put("call", call);
            record = db.insert("record", null, values);
            // 关闭数据库 释放数据库的链接
            db.close();
        }
        if (record < 0) {
            return false;
        } else {
            return true;
        }
    }

    /**
     * 查找数据库的操作
     */
    public boolean find(String name) {
        boolean result = false;
        SQLiteDatabase db = dbOpenHelper.getReadableDatabase();
        if (db.isOpen()) {
            //方法二
            Cursor cursor = db.query("record", null, "name=?",
                    new String[]{name}, null, null, null);

            //利用游标查询
            if (cursor.moveToFirst()) {
                int index = cursor.getColumnIndex("account"); // 得到phone在表中是第几列
                String account = cursor.getString(index);
                result = true;

            }
            // 记得关闭掉 cursor
            cursor.close();
            result = false;
            // 释放数据库的链接
            db.close();
        }
        return result;
    }

    /**
     * 查找数据库的操作
     */
    public List<ContactInfo> findByText(String text) {
        List<ContactInfo> contactInfos = new ArrayList<>();
        SQLiteDatabase db = dbOpenHelper.getReadableDatabase();
        if (db.isOpen()) {
            String sql = "select *" + " from " + "record"
                    + " where " + "account" + " like'%" + text + "%'";
            Cursor cursor = db.rawQuery(sql, null);
            //利用游标查询
            while (cursor.moveToNext()) {
                ContactInfo contactInfo = new ContactInfo();
                int nameIndex = cursor.getColumnIndex("name"); // 得到phone在表中是第几列
                int timeIndex = cursor.getColumnIndex("time"); // 得到phone在表中是第几列
                int accountIndex = cursor.getColumnIndex("account");
                int callDurationIndex = cursor.getColumnIndex("call");
                String name = cursor.getString(nameIndex);
                String time = cursor.getString(timeIndex);
                String account = cursor.getString(accountIndex);
                String callDutationIndex = cursor.getString(callDurationIndex);
                contactInfo.setName(name);
                contactInfo.setTime(time);
                contactInfo.setNumber(account);
                contactInfo.setCallDutation(callDutationIndex);
                contactInfos.add(contactInfo);
            }
            // 记得关闭掉 cursor
            cursor.close();
            // 释放数据库的链接
            db.close();
        }
        return contactInfos;
    }

    /**
     * 删除一条记录
     *
     * @param time
     */
    public boolean delete(String time) {
        SQLiteDatabase db = dbOpenHelper.getWritableDatabase();
        int record = 0;
        if (db.isOpen()) {
            //方法二
            record = db.delete("record", "time=?", new String[]{time});
            // 释放数据库的链接
            db.close();
        }
        if (record > 0) {
            return true;
        } else {
            return false;
        }
    }

    /**
     * 查找全部
     */
    public List<ContactInfo> getAllRecords() {
        List<ContactInfo> contacts = null;
        SQLiteDatabase db = dbOpenHelper.getReadableDatabase();
        if (db.isOpen()) {
            contacts = new ArrayList<>();
            //方法一
            Cursor cursor = db.rawQuery("select * from record ", null);
            while (cursor.moveToNext()) {
                ContactInfo contact = new ContactInfo();
                int nameindex = cursor.getColumnIndex("name");
                int accountindex = cursor.getColumnIndex("account");
                int timeIndex = cursor.getColumnIndex("time");
                int idInder = cursor.getColumnIndex("id");
                int callDurationIndex = cursor.getColumnIndex("call");
                String name = cursor.getString(nameindex);
                String account = cursor.getString(accountindex);
                String time = cursor.getString(timeIndex);
                String id = cursor.getString(idInder);
                String callDuration = cursor.getString(callDurationIndex);
                contact.setName(name);
                contact.setNumber(account);
                contact.setTime(time);
                contact.setId(id);
                contact.setCallDutation(callDuration);
                contacts.add(contact);
            }
            cursor.close();
            db.close();
        }
        Collections.reverse(contacts);
        return contacts;
    }

    public void clearRecording() {
        SQLiteDatabase db = dbOpenHelper.getReadableDatabase();
        if (db.isOpen()) {
            db.execSQL("delete from record");
            db.close();
        }
    }
}
